package controller

import (
	"gitee.com/song-yao-shun/MQH/dto"
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/response"
	"gitee.com/song-yao-shun/MQH/respository"
	"gitee.com/song-yao-shun/MQH/vo"
	"github.com/gin-gonic/gin"
	"strconv"
)

type TCaseCItf interface {
	RestController
}

type TCaseController struct {
	TCaseRty respository.TCaseRepository
}

func (T TCaseController) Create(ctx *gin.Context) {
	// 接收参数
	var voRequest vo.CreateTCase
	if err := ctx.ShouldBind(&voRequest); err != nil {
		response.Fail(ctx, nil, "数据验证错误！")
		return
	}

	// 判断节点是否存在
	n := respository.NewNodeRepository()
	_, err := n.SelectById(voRequest.NodeId)
	if err != nil {
		response.Fail(ctx, nil, "测试场景节点不存在！")
		return
	}

	// 创建 TestCase
	result := model.TCase{
		NodeId: 		voRequest.NodeId,
		OrderNum: 		voRequest.OrderNum,
		Name: 			voRequest.Name,
		Priority: 		voRequest.Priority,
		Precondition: 	voRequest.Precondition,
		TestData: 		voRequest.TestData,
		Procedure: 		voRequest.Procedure,
		ExpectedResult: voRequest.ExpectedResult,
		Img: 			voRequest.Img,
		Desc: 			voRequest.Desc,
	}

	resultId, err := T.TCaseRty.Create(result)
	if err != nil {
		response.Fail(ctx, nil, "新建失败！")
		return
	}

	// 返回
	response.Success(ctx, gin.H{"data":resultId}, "新建成功！")
}

func (T TCaseController) Update(ctx *gin.Context) {
	// 接收参数
	var voRequest vo.CreateTCase
	if err := ctx.ShouldBind(&voRequest); err != nil {
		response.Fail(ctx, nil, "数据验证错误！")
		return
	}

	// 获取 path 中的参数
	pathId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	updateResult, err := T.TCaseRty.SelectById(pathId)
	if err != nil {
		response.Fail(ctx, nil, "测试用例不存在！")
		return
	}

	// 判断变更后的 NodeId 是否有效
	n := respository.NewNodeRepository()
	_, err = n.SelectById(voRequest.NodeId)
	if err != nil {
		response.Fail(ctx, nil, "修改后的 测试节点 不存在！")
		return
	}

	// 更新后的内容依次赋值
	updateResult.NodeId 			= voRequest.NodeId
	updateResult.OrderNum 			= voRequest.OrderNum
	updateResult.Name 				= voRequest.Name
	updateResult.Priority 			= voRequest.Priority
	updateResult.Precondition 		= voRequest.Precondition
	updateResult.TestData 			= voRequest.TestData
	updateResult.Procedure 			= voRequest.Procedure
	updateResult.ExpectedResult 	= voRequest.ExpectedResult
	updateResult.Img 				= voRequest.Img
	updateResult.Desc 				= voRequest.Desc

	// update 不再逐个参数校验
	//// 判断变更后的 Name 是否变更
	//if voRequest.Name != "" && updateResult.Name != voRequest.Name{
	//	updateResult.Name = voRequest.Name
	//}
	//// Select all fields but omit Role (select all fields include zero value fields)
	//db.Model(&user).Select("*").Omit("Role").Update(User{Name: "jinzhu", Role: "admin", Age: 0})
	result, err := T.TCaseRty.Update(*updateResult)
	if err != nil {
		panic(err)
	}

	response.Success(ctx, gin.H{"data":result}, "修改成功。")

}

func (T TCaseController) Show(ctx *gin.Context) {

	// 获取 path 中的参数
	pathId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	result := T.TCaseRty.SelectFullTCaseById(pathId)
	if result.ID == 0 {
		response.Fail(ctx, nil, "测试用例不存在！")
		return
	}

	response.Success(ctx, gin.H{"data":result}, "")
}

func (T TCaseController) Delete(ctx *gin.Context) {

	// 获取 path 中的参数
	pathId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	if rowsAffected := T.TCaseRty.DeleteById(pathId);rowsAffected != 1 {
		response.Fail(ctx, nil, "删除失败，请重试！")
		return
	}

	response.Success(ctx, nil, "删除成功！")
}

func (T TCaseController) List(ctx *gin.Context) {
	// 接收参数
	var voList vo.TCases
	if err := ctx.ShouldBind(&voList); err != nil {
		response.Fail(ctx, nil, "数据验证错误，请检查提交的内容！")
		return
	}

	results, total := T.TCaseRty.List(voList)

	if voList.SelectType != ""{
		response.Success(ctx, gin.H{"data":dto.ToTCaseDto(results), "total":total}, "查询列表成功。")
		return
	}
	response.Success(ctx, gin.H{"data":results, "total":total}, "查询列表成功。")
}

func NewTCaseController() TCaseCItf{
	return TCaseController{TCaseRty: respository.NewTCaseRepository()}
}
